<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button type="danger" plain
      round
      size="mini"
      class="edit-btn"
      @click="isEdit = !isEdit">{{isEdit ? '完成' : '编辑'}}</van-button>
    </van-cell>
    <!-- 我的频道的内容  -->
    <van-grid class="my-grid" :gutter="10">
    <van-grid-item
    class="grid-item"
    @click="onMyChannelClick(channel,index)"
      v-for="(channel,index) in myChannels" :key="index">
       <!-- 获取到父组件中显示高亮的active的值
       对子组件的id和接收父组件的active 进行对比
       如果相等，那么动态绑定的class 就显示对应的值，更改高亮
        -->
        <van-icon
        v-show="isEdit && !fiexChannels.includes(channel.id)"
          slot="icon"
          name="clear"
        ></van-icon>
      <span
      :class="{ active: index === active}"
      class="text"
      slot="text">{{channel.name}}</span>
    </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <!-- 频道里面的内容  -->
    <van-grid class="recommend-grid" :gutter="10">
    <van-grid-item
    class="grid-item"
      v-for="(channel,index) in recommendChannels" :key="index"
      icon="plus"
      :text="channel.name"
      @click="onAddChannel(channel)"
       />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  data () {
    return {
      allChannels: [], // 所有频道
      isEdit: false, // 控制编辑状态的显示
      fiexChannels: [0] // 固定频道，不允许删除
    }
  },
  props: { // 接收父组件中传递过来的数组，进行我的频道遍历
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },
    // 添加频道
    // 添加原则： 已登录，请求借口；未登录，就存入本地
    async onAddChannel (channel) { // 点击讲推荐频道push 进我的频道
      this.myChannels.push(channel)
      this.$toast('数据添加成功')
      // 处理数据持久化,线上
      if (this.user) {
        try {
          await addUserChannel({
            id: channel.id, // 频道ID
            seq: this.myChannels.length // 序号
          })
        } catch (err) {
          this.$toast('保存失败，请稍后重试')
        }
        // 已登录，吧数据请求回来
        this.$toast('添加频道成功')
      } else {
        // 未登录，吧数据存储到本地
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onMyChannelClick (channel, index) {
      if (this.isEdit) {
        // 如果是固定频道，则不能删除
        if (this.fiexChannels.includes(channel.id)) {
          return
        }
        // 编辑状态， 执行删除频道
        if (index <= this.active) {
          // 让激活的频道的索引 - 1
          this.$emit('update-active', this.active - 1, true)
        }
        // 删除频道项
        this.myChannels.splice(index, 1)
        // 4、处理持久化
        this.deleteChannel(channel)
      } else {
        // 非编辑状态,执行切换
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
        // 已登录，则将数据更新到线上
          await deleteUserChannel(channel.id)
          this.$toast('删除频道成功')
        } else {
        // 未登录，将数据更新到本地
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('操作失败,请稍后重试')
      }
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      const channels = []
      this.allChannels.forEach(channel => {
        const ret = this.myChannels.find(myChannels => {
          return myChannels.id === channel.id
        })
        // 如果我的频道中不包括该频道项，则收集到推荐频道中
        if (!ret) {
          channels.push(channel)
        }
      })
      // 吧计算结果返回
      return channels
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {  //设置编辑按钮样式
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .grid-item {  //设置频道推荐内容样式
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;  //不让文字折行
      background-color: #f4f5f6;
      .van-grid-item__text, .text {
        font-size: 28px;
        color: black;
        margin-top: 0;
      }
      .active {
        color: pink;
      }
      .van-grid-item__icon-wrapper { // 去掉删除图标父元素的定位
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
  /deep/ .recommend-grid {  //设置里面图标的样式
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
